package org.java.order.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.concurrent.TimeUnit;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-19 0019 下午 3:24
 */
@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean existKey(String key) {
//        log.debug("existKey().params:" + key);
        return stringRedisTemplate.hasKey(key);
    }

    public Boolean delKey(String key) {
//        log.debug("delKey().params:" + key);
        Boolean delete = stringRedisTemplate.delete(key);
        return delete;
    }

    public boolean addString(String key, String value) {
        BoundValueOperations<String, String> ops = stringRedisTemplate.boundValueOps(key);
        try {
            ops.set(value);
            return true;
        } catch (Exception e) {
//            log.error("redis添加异常." + e.getMessage(), e);
            e.printStackTrace();
            return false;
        }
    }

    public boolean addString(String key, String value, long timeout, TimeUnit timeUnit) {

        BoundValueOperations<String, String> ops = stringRedisTemplate.boundValueOps(key);
        try {
            ops.set(value, timeout, timeUnit);
            return true;
        } catch (Exception e) {
//            log.error("redis添加异常." + e.getMessage(), e);
            e.printStackTrace();
            return false;
        }
    }

}
